home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection 1998 Fall: Game Toolkit / Disc.iso / SDKs / PCI Driver Development Kit / • Tools / Utility / DisplayNameRegistry 950412 / Src / DisplayNameRegistry.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-02-27  |  18.7 KB  |  828 lines  |  [TEXT/MPCC]

  1. /*                            NameRegistryDisplayMain.c                            */
  2. /*
  3.  * NameRegistryDisplayMain.c
  4.  * Copyright © 1993-95 Apple Computer Inc. All rights reserved.
  5.  * Edit History
  6.  * 1.0a3 (94.12.23)        Release for A5 DDK
  7.  * 1.0a4 (94.12.30)        Format "reg" property.
  8.  * 1.0a5 (95.01.19)        Fix bug: "save doesn't write anything" Correctly
  9.  *                        dispose of the primary and secondary lists.
  10.  * 1.0b1 (95.03.23)        Fix bug: it is possible to have two independent registry
  11.  *                        entries with the same name. Fix the sort algorithm so
  12.  *                        they aren't smooshed together.
  13.  */
  14. #define EXTERN            /* Allocate variables */
  15.  
  16. #include "DisplayNameRegistry.h"
  17. #if (defined(__powerc) || defined(powerc)) && defined(__MWERKS__) == 0
  18. /*
  19.  * Power PC does not automatically define the QuickDraw globals. This is because
  20.  * only "application" code-fragments need these globals, and this cannot be
  21.  * determined before the code fragment is constructed.
  22.  */
  23. QDGlobals        qd;            /* This is not automatically defined on PowerPC        */
  24. #endif
  25. Boolean            gSaveAllElements = FALSE;
  26.  
  27. /*
  28.  * These enum's define items in the various menus.
  29.  */
  30. enum AppleMenu {
  31.     kAppleAbout                = 1
  32. };
  33. enum {                                            /* File Menu                    */
  34.     kFileSaveAs                = 1,
  35.     kFileUnused1,
  36.     kFilePageSetup,
  37.     kFilePrint,
  38.     kFileUnused2,
  39.     kFileQuit
  40. };
  41. enum EditMenu {
  42.     kEditUndo                = 1,
  43.     kEditUnused,
  44.     kEditCut,
  45.     kEditCopy,
  46.     kEditPaste,
  47.     kEditClear
  48. };
  49. enum OptionMenu {
  50.     kOptionSortByName        = 1,
  51.     kOptionSortByProperty,
  52.     kOptionUnused1,
  53.     kOptionRefreshDisplay,
  54.     kOptionSetFontInfo
  55. };
  56.  
  57. Boolean                    gInForeground;
  58. long                    gSleepTime;
  59.  
  60. /*
  61.  * Local function prototypes.
  62.  */
  63. void                        main(void);
  64. void                        ProcessThisEvent(
  65.         const EventRecord        *eventRecordPtr,
  66.         long                    *sleepTimePtr
  67.     );
  68. void                        ApplicationEventLoop(void);
  69. void                        DoMouseEvent(
  70.         const EventRecord        *eventRecordPtr
  71.     );
  72. void                        DoCommand(
  73.         WindowPtr                activeWindow,
  74.         long                    menuChoice
  75.     );
  76. void                        AdjustMenus(void);
  77. void                        InitMacintosh(void);
  78. void                        InitApplication(void);
  79. void                        AdjustEditMenu(
  80.         Boolean                    isDeskAcc
  81.     );
  82. static Boolean                IsOurWindow(
  83.         WindowPtr                theWindow
  84.     );
  85. void                        DoPageSetup(void);
  86. void                        DoAbout(void);
  87. pascal OSErr                MyAEOpenAppHandlerFunc(
  88.         long                    refCon
  89.     );
  90. pascal OSErr                MyAEQuitAppHandlerFunc(
  91.         long                    refCon
  92.     );
  93. pascal void                    MyAEUnknownHandlerMsgFunc(
  94.         ConstStr255Param        messageText
  95.     );
  96. pascal Boolean                MyAEIdleProc(
  97.         const EventRecord        *theEventPtr,
  98.         long                    *sleepTime,
  99.         RgnHandle                *mouseRgn
  100.     );
  101. RoutineDescriptor        gAEIdleUPP =
  102.             BUILD_ROUTINE_DESCRIPTOR(uppAEIdleProcInfo, MyAEIdleProc);
  103.  
  104. /*
  105.  * main
  106.  * The application main program. This is a limited program for the twist-down
  107.  * sample; it is not intended as a model for a complete Macintosh program.
  108.  */
  109. void
  110. main()
  111. {
  112.         OSErr                            status;
  113.         
  114.         InitMacintosh();
  115.         InitApplication();
  116.         status = InitializeAppleEvents(
  117.                     MyAEOpenAppHandlerFunc,
  118.                     NULL,                                /* No open doc handler    */
  119.                     NULL,                                /* No print doc    handler */
  120.                     MyAEQuitAppHandlerFunc,
  121.                     NULL,                                /* No DoScript callback    */
  122.                     MyAEUnknownHandlerMsgFunc,
  123.                     0                                    /* No refCon            */
  124.                 );
  125.         if (status != noErr) {
  126.             if (status == gestaltUndefSelectorErr)        /* Huh? no AppleEvents?    */
  127.                 status = MakeNameRegistryBrowserWindow();
  128.             if (status == noErr)
  129.                 FatalError(status, "\pCan't initialize AppleEvents");
  130.         }
  131.         gInForeground = TRUE;
  132.         gSleepTime = 0;
  133.         InitCursor();
  134.         while (gQuitNow == FALSE) {
  135.             if (gUpdateMenusNeeded)
  136.                 AdjustMenus();
  137.             WaitNextEvent(everyEvent, &gEventRecord, gSleepTime, NULL);
  138.             gAECoreGlobals.currentEventIsAppleEvent = FALSE;
  139.             ProcessThisEvent(&gEventRecord, &gSleepTime);
  140.         }
  141. }
  142.  
  143. pascal OSErr
  144. MyAEOpenAppHandlerFunc(
  145.         long                    refCon
  146.     )
  147. {
  148.         OSErr                    status;
  149.         
  150.         UNUSED(refCon);
  151.         status = MakeNameRegistryBrowserWindow();
  152.         gUpdateMenusNeeded = TRUE;
  153.         return (status);
  154. }
  155.  
  156. pascal OSErr
  157. MyAEQuitAppHandlerFunc(
  158.         long                    refCon
  159.     )
  160. {
  161.         UNUSED(refCon);
  162.         gQuitNow = TRUE;
  163.         return (noErr);
  164. }
  165.  
  166. pascal void
  167. MyAEUnknownHandlerMsgFunc(
  168.         ConstStr255Param        messageText
  169.     )
  170. {
  171.         NonFatalError(errAEHandlerNotFound, messageText);
  172. }
  173.  
  174. void
  175. ProcessThisEvent(
  176.         const EventRecord        *eventRecordPtr,
  177.         long                    *sleepTimePtr
  178.     )
  179. {
  180.         OSErr                    status;
  181.         long                    menuChoice;
  182.         register WindowPtr        theWindow;
  183.         GrafPtr                    savePort;
  184.         Boolean                    isActivating;
  185. #define EVENT    (*eventRecordPtr)
  186.  
  187.         theWindow = FrontWindow();
  188.         switch (EVENT.what) {
  189.         case nullEvent:
  190.             break;
  191.         case keyDown:
  192.         case autoKey:
  193.             if ((EVENT.message & charCodeMask) == '.'
  194.              && (EVENT.modifiers & cmdKey) != 0) {
  195.                 FlushEvents(keyDown | autoKey, 0);
  196.                 gQuitNow = TRUE;
  197.             }
  198.             else if ((EVENT.modifiers & cmdKey) != 0) {
  199.                 if (EVENT.what == keyDown) {
  200.                     menuChoice = MenuKey(EVENT.message & charCodeMask);
  201.                     if (HiWord(menuChoice) != 0 && IsOurWindow(theWindow))
  202.                         DoCommand(theWindow, menuChoice);
  203.                     else if (IsOurWindow(theWindow))
  204.                         DoWindowKeyDown((BrowserPtr) theWindow);
  205.                     else {
  206.                         SysBeep(10);
  207.                     }
  208.                 }
  209.             }
  210.             else if (IsOurWindow(theWindow))
  211.                 DoWindowKeyDown((BrowserPtr) theWindow);
  212.             else {
  213.                 SysBeep(10);
  214.             }
  215.             break;
  216.         case mouseDown:
  217.             DoMouseEvent(eventRecordPtr);
  218.             break;
  219.         case updateEvt:
  220.             theWindow = (WindowPtr) EVENT.message;
  221.             GetPort(&savePort);
  222.             SetPort(theWindow);
  223.             BeginUpdate(theWindow);
  224.             EraseRect(&theWindow->portRect);
  225.             DrawGrowIcon(theWindow);
  226.             DrawControls(theWindow);
  227.             if (IsOurWindow(theWindow))
  228.                 UpdateBrowserWindow((BrowserPtr) theWindow, theWindow->visRgn);
  229.             EndUpdate(theWindow);
  230.             SetPort(savePort);
  231.             break;
  232.         case activateEvt:
  233.             theWindow = (WindowPtr) EVENT.message;
  234.             isActivating = ((EVENT.modifiers & activeFlag) != 0);
  235.             goto activateEvent;
  236.             break;
  237.         case kHighLevelEvent:
  238.             status = AEProcessAppleEvent(&EVENT);
  239.             switch (status) {
  240.             case userCanceledErr:
  241.             case errAEEventNotHandled:
  242.             case noErr:
  243.                 break;
  244.             default:
  245.                 NonFatalError(status, "\pAppleEvent handler error");
  246.                 break;
  247.             }
  248.             break;
  249.         case osEvt:
  250.             switch (((unsigned long) EVENT.message) >> 24) {
  251.             case mouseMovedMessage:
  252.                 break;
  253.             case suspendResumeMessage:
  254.                 isActivating = ((EVENT.message & 0x01) != 0);
  255. activateEvent:    if (isActivating) {
  256.                     /*
  257.                      * Activate this window. Activate events define theWindow from
  258.                      * the event record, while suspend/resume uses the pre-set
  259.                      * FrontWindow value.
  260.                      */
  261.                     SelectWindow(theWindow);
  262.                     SetPort(theWindow);
  263.                     (void) TEFromScrap();
  264.                 }
  265.                 if (IsOurWindow(theWindow)) {
  266.                     ActivateBrowser(
  267.                         (BrowserPtr) theWindow,
  268.                         isActivating
  269.                     );
  270.                     /*
  271.                      * Globalize the current window for the debugger's convenience.
  272.                      */
  273.                     if (isActivating)
  274.                         gCurrentBrowserPtr = (BrowserPtr) theWindow;
  275.                 }
  276.                 else {
  277.                     /* Desk accessory or what? */
  278.                 }
  279.                 gInForeground = isActivating;
  280.                 *sleepTimePtr = (gInForeground) ? 6 : 60;
  281.                 gUpdateMenusNeeded = TRUE;
  282.                 break;
  283.             }
  284.             break;
  285.         }
  286. #undef EVENT
  287. }
  288.  
  289. /*
  290.  * DoMouseEvent
  291.  * The user clicked on something. Handle application-wide processing here, or call
  292.  * a Browser function for specific action.
  293.  */
  294. void
  295. DoMouseEvent(
  296.         const EventRecord        *eventRecordPtr
  297.     )
  298. {
  299.         WindowPtr                theWindow;
  300.         short                    whichPart;
  301. #define EVENT    (*eventRecordPtr)
  302.         
  303.         whichPart = FindWindow(EVENT.where, &theWindow);
  304.         if (theWindow == NULL)
  305.             theWindow = FrontWindow();
  306.         if (whichPart == inMenuBar && IsOurWindow(theWindow) == FALSE)
  307.             theWindow = FrontWindow();
  308.         switch (whichPart) {
  309.         case inDesk:
  310.             break;
  311.         case inMenuBar:
  312.             InitCursor();
  313.             DoCommand(theWindow, MenuSelect(EVENT.where));
  314.             break;
  315.         case inDrag:
  316.             DragWindow(theWindow, EVENT.where, &qd.screenBits.bounds);
  317.             break;
  318.         case inZoomIn:
  319.         case inZoomOut:
  320.             if (IsOurWindow(theWindow)
  321.              && TrackBox(theWindow, EVENT.where, whichPart)) {
  322.                  DoZoomWindow(theWindow, whichPart);
  323.                  goto resizeWindow;
  324.             }
  325.             break;
  326.         case inGrow:
  327.             if (IsOurWindow(theWindow)
  328.              && DoGrowWindow(
  329.                          theWindow,
  330.                          EVENT.where,
  331.                          kMinWindowWidth,
  332.                          kMinWindowHeight)) {
  333. resizeWindow:    DecorateBrowserWindow((BrowserPtr) theWindow);
  334.             }
  335.             break;
  336.         case inGoAway:
  337.             if (TrackGoAway(theWindow, EVENT.where)) {
  338.                 if (IsOurWindow(theWindow)) {
  339.                     DisposeBrowser((BrowserPtr) theWindow);
  340.                     if (gOpenWindowCount <= 0)
  341.                         gQuitNow = TRUE;
  342.                 }
  343.                 else {
  344.                     SysBeep(10);
  345.                 }
  346.             }
  347.             break;
  348.         case inContent:
  349.             if (theWindow != FrontWindow())
  350.                 SelectWindow(theWindow);
  351.             else if (IsOurWindow(theWindow)) {
  352.                 DoContentClick((BrowserPtr) theWindow, eventRecordPtr);
  353.             }
  354.             else {
  355.                 /* Nothing happens here        */
  356.             }
  357.             break;
  358.         default:
  359.             break;                            /* Bogus click: ignore                */
  360.         }
  361. #undef EVENT
  362. }
  363.  
  364. /*
  365.  * DoCommand
  366.  * Process a menu or keystroke command.
  367.  */
  368. void
  369. DoCommand(
  370.         WindowPtr                theWindow,
  371.         long                    menuChoice
  372.     )
  373. {
  374.         short                    menuItem;
  375.         Str255                    menuText;
  376.         GrafPtr                    savePort;
  377.         BrowserPtr                browserPtr;
  378.         Boolean                    oldSortByName;
  379.         
  380.         menuItem = LoWord(menuChoice);
  381.         switch (HiWord(menuChoice)) {
  382.         case MENU_Apple:
  383.             if (menuItem == kAppleAbout)
  384.                 DoAbout();
  385.             else {
  386.                 GetMenuItemText(gAppleMenu, menuItem, menuText);
  387.                 AdjustEditMenu(TRUE);
  388.                 GetPort(&savePort);
  389.                 OpenDeskAcc(menuText);
  390.                 SetPort(savePort);
  391.                 AdjustEditMenu(IsOurWindow(theWindow) == FALSE);
  392.             }
  393.             break;
  394.         case MENU_File:
  395.             switch (menuItem) {
  396.             case kFileSaveAs:
  397.                 CreateOutputFile();
  398.                 break;
  399.             case kFilePageSetup:
  400.                 DoPageSetup();
  401.                 break;
  402.             case kFilePrint:
  403.                 if (IsOurWindow(theWindow))
  404.                     PrintBrowserWindow((BrowserPtr) theWindow);
  405.                 break;
  406.             case kFileQuit:
  407.                 gQuitNow = TRUE;
  408.                 break;
  409.             }
  410.             break;
  411.         case MENU_Edit:
  412.             if (SystemEdit(menuItem - 1) == FALSE)
  413.                 SysBeep(10);
  414.             break;
  415.         case MENU_Options:
  416.             if (IsOurWindow(theWindow)) {
  417.                 browserPtr = (BrowserPtr) theWindow;
  418.                 oldSortByName = BROWSER.sortByName;
  419.                 switch (menuItem) {
  420.                 case kOptionSortByName:
  421.                     BROWSER.sortByName = TRUE;
  422.                     break;
  423.                 case kOptionSortByProperty:
  424.                     BROWSER.sortByName = FALSE;
  425.                     break;
  426.                 case kOptionRefreshDisplay:
  427.                     DoRefreshDisplay(browserPtr);
  428.                     break;
  429.                 case kOptionSetFontInfo:
  430.                     if (AEInteractionOK((AEIdleUPP) &gAEIdleUPP)
  431.                      && SetFontInfoDialog(browserPtr))
  432.                         DoSetFontInfo(browserPtr);
  433.                     break;
  434.                 }
  435.                 if (oldSortByName != BROWSER.sortByName) {
  436.                     gUpdateMenusNeeded = TRUE;
  437.                     SortAndDisplayBrowserWindow(browserPtr);
  438.                 }
  439.             }
  440.             break;
  441.         }
  442.         HiliteMenu(0);
  443. }        
  444.  
  445. /*
  446.  * AdjustMenus
  447.  * Enable/disable menu options.
  448.  */
  449. void
  450. AdjustMenus(void)
  451. {
  452.         register BrowserPtr        browserPtr;
  453.         Boolean                    isOurWindow;
  454.  
  455.         isOurWindow = IsOurWindow(FrontWindow());
  456.         EnableItem(gAppleMenu, kAppleAbout);
  457.         EnableItem(gFileMenu, kFileQuit);
  458.         EnableItem(gFileMenu, kFilePageSetup);
  459.         AdjustEditMenu(isOurWindow == FALSE);
  460.         if (isOurWindow) {
  461.             browserPtr = (BrowserPtr) FrontWindow();
  462.             EnableItem(gOptionMenu, 0);
  463.             EnableItem(gFileMenu, kFileSaveAs);
  464.             EnableItem(gFileMenu, kFilePrint);
  465.             CheckItem(gOptionMenu, kOptionSortByName, BROWSER.sortByName);
  466.             CheckItem(gOptionMenu, kOptionSortByProperty, !BROWSER.sortByName);
  467.         }
  468.         else {
  469.             DisableItem(gOptionMenu, 0);
  470.             DisableItem(gFileMenu, kFileSaveAs);
  471.             DisableItem(gFileMenu, kFilePrint);
  472.         }
  473. }
  474.  
  475. /*
  476.  * AdjustEditMenu
  477.  * Enable/disable Edit Menu options.
  478.  */
  479. void
  480. AdjustEditMenu(
  481.         Boolean                    isDeskAcc
  482.     )
  483. {
  484.         if (isDeskAcc) {
  485.             EnableItem(gEditMenu, kEditUndo);
  486.             EnableItem(gEditMenu, kEditCut);
  487.             EnableItem(gEditMenu, kEditCopy);
  488.             EnableItem(gEditMenu, kEditPaste);
  489.             EnableItem(gEditMenu, kEditClear);
  490.         }
  491.         else {
  492.             DisableItem(gEditMenu, kEditUndo);
  493.             DisableItem(gEditMenu, kEditCut);
  494.             DisableItem(gEditMenu, kEditCopy);
  495.             DisableItem(gEditMenu, kEditPaste);
  496.             DisableItem(gEditMenu, kEditClear);
  497.         }
  498. }
  499.  
  500. /*
  501.  * InitMacintosh
  502.  * Perform the normal application initialization. This must be extended for "real"
  503.  * applications. The only thing this module does is initialize the managers.
  504.  */
  505. void
  506. InitMacintosh(void)
  507. {
  508.         int                        i;
  509.         
  510.         MaxApplZone();        
  511.         InitGraf(&qd.thePort);
  512.         InitFonts();
  513.         InitWindows();
  514.         InitMenus();
  515.         TEInit();
  516.         InitDialogs(0);
  517.         HNoPurge((Handle) GetCursor(watchCursor));
  518.         SetCursor(*GetCursor(watchCursor));
  519.         for (i = 0; i < 3; i++)
  520.             EventAvail(everyEvent, &gEventRecord);
  521. }
  522.  
  523. /*
  524.  * InitApplication
  525.  * Continue initialization.
  526.  */
  527. void
  528. InitApplication(void)
  529. {        
  530.         (void) TEFromScrap();
  531.         SetMenuBar(GetNewMBar(MBAR_MenuBar));
  532.         gAppleMenu = GetMenuHandle(MENU_Apple);
  533.         gFileMenu = GetMenuHandle(MENU_File);
  534.         gEditMenu = GetMenuHandle(MENU_Edit);
  535.         gOptionMenu = GetMenuHandle(MENU_Options);
  536.         AppendResMenu(GetMenuHandle(MENU_Apple), 'DRVR');
  537.         DrawMenuBar();
  538.         SetupAnimatedCursor(ACUR_Animator);
  539. }
  540.  
  541. /*
  542.  * IsOurWindow
  543.  * Return TRUE if this is a browser window.
  544.  */
  545. Boolean
  546. IsOurWindow(
  547.         WindowPtr                theWindow
  548.     )
  549. {
  550.         if (theWindow == NULL
  551.          || ((WindowPeek) theWindow)->windowKind != userKind)
  552.             return (FALSE);
  553.         else {
  554.             return (TRUE);
  555.         }
  556. }
  557.  
  558. void
  559. pstrcpy(
  560.         StringPtr                dst,
  561.         ConstStr255Param        src
  562.     )
  563. {
  564.         BlockMoveData(src, dst, src[0] + 1);
  565. }
  566.  
  567. void
  568. pstrcat(
  569.         StringPtr                dst,
  570.         ConstStr255Param        src
  571.     )
  572. {
  573.         short                    length;
  574.         
  575.         length = 255 - dst[0];
  576.         if (length > src[0])
  577.             length = src[0];
  578.         BlockMoveData(&src[1], &dst[1] + dst[0], length);
  579.         dst[0] += length;
  580. }
  581.  
  582. /*
  583.  * DoZoomWindow
  584.  * Algorithm from New Inside Mac (Toolbox Essentials) 4-55
  585.  */
  586. void
  587. DoZoomWindow(
  588.         WindowPtr                theWindow,
  589.         short                    whichPart
  590.     )
  591. {
  592.         GDHandle                gd;
  593.         GDHandle                gdZoom;
  594.         GrafPtr                    savePort;
  595.         Rect                    windowRect;
  596.         Rect                    zoomRect;
  597.         Rect                    intersection;
  598.         long                    thisArea;
  599.         long                    greatestArea;
  600.         short                    windowTitleHeight;
  601.         long                    response;
  602.         Boolean                    hasColorQuickDraw;
  603. #define PEEK    (*((WindowPeek) theWindow))
  604. #define STATE    (**((WStateDataHandle) PEEK.dataHandle))
  605.  
  606.         GetPort(&savePort);
  607.         SetPort(theWindow);
  608.  
  609.         hasColorQuickDraw = FALSE;
  610.         if (Gestalt(gestaltQuickdrawVersion, &response) == noErr
  611.          && response >= gestalt8BitQD)
  612.              hasColorQuickDraw = TRUE;
  613.         if (whichPart == inZoomOut) {
  614.             if (hasColorQuickDraw == FALSE) {
  615.                 /*
  616.                  * This shouldn't happen on a modern system, but, if it does, just
  617.                  * force a single screen zoom.
  618.                  */
  619.                 zoomRect = qd.screenBits.bounds;
  620.                 InsetRect(&zoomRect, 4, 4);
  621.                 STATE.stdState = zoomRect;
  622.             }
  623.             else {
  624.                 /*
  625.                  * We have color QuickDraw. Locate the screen that contains
  626.                  * the largest area of the window and zoom to that screen.
  627.                  */
  628.                 windowRect = theWindow->portRect;
  629.                 LocalToGlobal(&topLeft(windowRect));
  630.                 LocalToGlobal(&botRight(windowRect));
  631.                 windowTitleHeight = windowRect.top
  632.                         - 1
  633.                         - (**PEEK.strucRgn).rgnBBox.top;
  634.                 windowRect.top -= windowTitleHeight;
  635.                 greatestArea = 0;
  636.                 gdZoom = NULL;
  637.                 /*
  638.                  * Look at all graphics devices and find an intersection. Then
  639.                  * select the largest intersecting screen.
  640.                  */
  641.                 for (gd = GetDeviceList(); gd != NULL; gd = GetNextDevice(gd)) {
  642.                     if (TestDeviceAttribute(gd, screenDevice)
  643.                      && TestDeviceAttribute(gd, screenActive)
  644.                      && SectRect(&windowRect, &(**gd).gdRect, &intersection)) {
  645.                         thisArea = ((long) width(intersection))
  646.                                  * ((long) height(intersection));
  647.                         if (thisArea > greatestArea) {
  648.                             greatestArea = thisArea;
  649.                             gdZoom = gd;
  650.                         }
  651.                     }
  652.                 }
  653.                 /*
  654.                  * If we're zooming to the device with the menu bar,
  655.                  * allow for its height.
  656.                  */
  657.                 if (GetMainDevice() == gdZoom)
  658.                     windowTitleHeight += GetMBarHeight();
  659.                 zoomRect = (**gdZoom).gdRect;
  660.                 InsetRect(&zoomRect, 3, 3);
  661.                 zoomRect.top += windowTitleHeight;
  662.                 STATE.stdState = zoomRect;
  663.             }                                        /* End if color QuickDraw    */
  664.         }                                            /* End if zoom out            */
  665.         ZoomWindow(theWindow, whichPart, (theWindow == FrontWindow()));
  666.         /*
  667.          * Zoom redraws the entire window.
  668.          */
  669.         EraseRect(&theWindow->portRect);
  670.         InvalRect(&theWindow->portRect);
  671.         SetPort(savePort);            
  672. #undef PEEK
  673. #undef STATE
  674. }
  675.  
  676. /*
  677.  * DoGrowWindow
  678.  * Algorithm from New Inside Mac (Toolbox Essentials) 4-58 (simplified)
  679.  * We assume that the window can cover the entire screen.
  680.  */
  681. Boolean
  682. DoGrowWindow(
  683.         WindowPtr                theWindow,
  684.         Point                    startingPoint,
  685.         short                    minimumWidth,
  686.         short                    minimumHeight
  687.     )
  688. {
  689.         long                    growSize;
  690.         Rect                    limitRect;
  691.         
  692.         limitRect.left = minimumWidth;
  693.         limitRect.top = minimumHeight;
  694.         limitRect.right = qd.screenBits.bounds.right;
  695.         limitRect.bottom = qd.screenBits.bounds.bottom;
  696.         growSize = GrowWindow(theWindow, startingPoint, &limitRect);
  697.         if (growSize != 0) {
  698.             SizeWindow(theWindow, LoWord(growSize), HiWord(growSize), TRUE);
  699.             /*
  700.              * Force a redraw of the entire window, (The correct algorithm
  701.              * excludes the intersection of the old view rect and the new view
  702.              * rect, but this doesn't work correctly yet.) Invalidate any prior
  703.              * update region in any case.
  704.              */
  705.             EraseRect(&theWindow->portRect);
  706.             InvalRect(&theWindow->portRect);
  707.         }
  708.         return (growSize != 0);
  709. }
  710.  
  711. void
  712. DoPageSetup(void)
  713. {
  714.         OSErr                    status;
  715.         
  716.         PrOpen();
  717.         status = PrError();
  718.         if (status != noErr)
  719.             NonFatalError(status, "\pPrinting disabled");
  720.         else {
  721.             if (gPrintHandle == NULL) {
  722.                 gPrintHandle = (THPrint) NewHandle(sizeof (TPrint));
  723.                 if (gPrintHandle != NULL)
  724.                     PrintDefault(gPrintHandle);
  725.             }
  726.             if (gPrintHandle != NULL)
  727.                 (void) PrStlDialog(gPrintHandle);
  728.             PrClose();
  729.         }
  730. }
  731.  
  732. void
  733. DoAbout(void)
  734. {
  735.         GrafPtr                    savePort;
  736.         DialogPtr                dialog;
  737.         short                    dialogItem;
  738.  
  739.         dialog = GetNewDialog(DLOG_About, NULL, (WindowPtr) -1L);
  740.         if (dialog != NULL) {
  741.             GetPort(&savePort);
  742.             SetPort(dialog);
  743.             ShowWindow(dialog);
  744.             ModalDialog(NULL, &dialogItem);
  745.             DisposeDialog(dialog);
  746.             SetPort(savePort);
  747.         }
  748. }
  749.  
  750. void
  751. CheckError(
  752.         OSErr                    errorStatus,
  753.         ConstStr255Param        errorMsg
  754.     )
  755. {
  756.         if (errorStatus != noErr && errorStatus != userCanceledErr)
  757.             NonFatalError(errorStatus, errorMsg);
  758. }
  759.  
  760. void
  761. NonFatalError(
  762.         OSErr                    errorStatus,
  763.         ConstStr255Param        errorMsg
  764.     )
  765. {
  766.         if (ErrorMessage(ALRT_NonFatalError, errorStatus, errorMsg) == kOKButton)
  767.             gQuitNow = TRUE;
  768. }
  769.  
  770. void
  771. FatalError(
  772.         OSErr                    errorStatus,
  773.         ConstStr255Param        errorMsg
  774.     )
  775. {
  776.         ErrorMessage(ALRT_FatalError, errorStatus, errorMsg);
  777.         ExitToShell();
  778. }
  779.  
  780. short
  781. ErrorMessage(
  782.         short                    alertID,
  783.         OSErr                    errorStatus,
  784.         ConstStr255Param        errorMsg
  785.     )
  786. {
  787.         Handle                    errorTextHdl;
  788.         StringPtr                errorTextPtr;
  789.         Str15                    errorStatusText;
  790.         short                    result;
  791.         
  792.         if (AEInteractionOK((AEIdleUPP) &gAEIdleUPP) == FALSE)
  793.             result = kCancelButton;
  794.         else {
  795.             NumToString(errorStatus, errorStatusText);
  796.             errorTextHdl = GetResource('Estr', errorStatus);
  797.             if (errorTextHdl != NULL) {
  798.                 HLock(errorTextHdl);
  799.                 errorTextPtr = (StringPtr) errorTextHdl;
  800.             }
  801.             else {
  802.                 errorTextPtr = "\pSystem Error";
  803.             }
  804.             ParamText(errorStatusText, errorTextPtr, errorMsg, "\p");
  805.             InitCursor();
  806.             result = StopAlert(alertID, NULL);
  807.             if (errorTextHdl != NULL)
  808.                 ReleaseResource(errorTextHdl);
  809.         }
  810.         return (result);
  811. }
  812.  
  813. pascal Boolean
  814. MyAEIdleProc(
  815.         const EventRecord        *theEventPtr,
  816.         long                    *sleepTime,
  817.         RgnHandle                *mouseRgn
  818.     )
  819. {
  820.         UNUSED(mouseRgn);
  821.         if (theEventPtr->what == kHighLevelEvent)
  822.             return (TRUE);
  823.         else {
  824.             ProcessThisEvent(theEventPtr, sleepTime);    /* Application specific    */
  825.             return (FALSE);
  826.         }
  827. }        
  828.